#include "GameData.h"
#include <cstddef>
#include "CommonImage.h"

static GameData * g_GameData = NULL;

#define INIT_FILE_PATH "init.plist"
#define THEME_NAME "name"
#define THEME_THUMB_PATH "thumb_path"

#define THEME_SCENE "scene"
#define THEME_SCENE_NAME "name"
#define THEME_SCENE_THUMB_PATH "thumb_path"
#define THEME_SCENE_MUSIC "music"

#define INGORE_ITEMS 2 //detemine by init.plist
#define MUSIC_PATH "music_path"
#define EFFECT_PATH "effect_path"

GameData::GameData() :
		m_pDataDictionary(0), m_allImages(0) {
	m_allImages = CCArray::create();
	m_allImages->retain();
}

GameData::~GameData() {
	m_pDataDictionary->release();
	m_allImages->release();

	CC_SAFE_DELETE_ARRAY(m_pThemeName);
	CC_SAFE_DELETE_ARRAY(m_pThemes);
}

int GameData::getSceneIdByName(const char * theme, const char * name) {
	int result = 0;
	return result;
}

int GameData::getThemeItemCount() {
	return m_themeCount;
}

ThemeItem * GameData::getThemeItem(int index) {
	return m_pThemes[index];
}

void GameData::initData() {
	m_pDataDictionary = CCDictionary::createWithContentsOfFile(INIT_FILE_PATH);
	m_pDataDictionary->retain();
	int count = m_pDataDictionary->count() - INGORE_ITEMS;

	m_themeCount = count;
	m_pThemeName = new CCString *[count]();
	char str[64];
	for (int i = 0; i < count; i++) {
		sprintf(str, "%d", i);
		m_pThemeName[i] = (CCString *) m_pDataDictionary->valueForKey(str);
	}

	m_musicPath = (CCString *) m_pDataDictionary->valueForKey(MUSIC_PATH);
	m_effectPath = (CCString *) m_pDataDictionary->valueForKey(EFFECT_PATH);

	CommonImage * commonImage = 0;

	m_pThemes = new ThemeItem*[count];

	for (int i = 0; i < count; i++) {
		CCDictionary * tmpDictionary = CCDictionary::createWithContentsOfFile(
				m_pThemeName[i]->getCString());
		m_pThemes[i] = new ThemeItem();

		m_pThemes[i]->setName(
				tmpDictionary->valueForKey(THEME_NAME)->getCString());
		m_pThemes[i]->setThumbPath(
				tmpDictionary->valueForKey(THEME_THUMB_PATH)->getCString());

		commonImage = new CommonImage(
				tmpDictionary->valueForKey(THEME_THUMB_PATH)->getCString());
		m_allImages->addObject(commonImage);

		initThemeItem(
				((CCDictionary*) tmpDictionary->objectForKey(THEME_SCENE)),
				m_pThemes[i]);
	}

}

void GameData::initThemeItem(CCDictionary * itemSceneDic, ThemeItem * theme) {
	char str[64];

	CCArray * array = itemSceneDic->allKeys();

	CCObject * object;
	CCARRAY_FOREACH(array, object)
	{
		CCString * key = (CCString *) object;
		CCDictionary * sceneDic = (CCDictionary *) itemSceneDic->objectForKey(
				key->getCString());

		ThemeItemScene * itemScene = new ThemeItemScene();
		itemScene->setName(
				sceneDic->valueForKey(THEME_SCENE_NAME)->getCString());
		itemScene->setThumbPath(
				sceneDic->valueForKey(THEME_SCENE_THUMB_PATH)->getCString());
		itemScene->setMusic(
				sceneDic->valueForKey(THEME_SCENE_MUSIC)->getCString());
		theme->addThemeSceneItem(itemScene);

		CommonImage * commonImage = new CommonImage(
				sceneDic->valueForKey(THEME_SCENE_THUMB_PATH)->getCString());
		m_allImages->addObject(commonImage);

	}
}

GameData * GameData::getInstance() {
	if (!g_GameData) {
		g_GameData = new GameData();
		g_GameData->initData();
	}
	return g_GameData;
}

CCArray * GameData::getAllImages() {
	return m_allImages;
}

void GameData::setCurrentThemeId(int id) {
	m_currentThemeId = id;
}

void GameData::setCurrentThemeItemId(int id) {
	m_currentThemeItemId = id;
}

const char * GameData::getNextThemeItem() {
	m_currentThemeItemId++;

	ThemeItem * themeItem = m_pThemes[m_currentThemeId];
	CCArray * array = themeItem->getThemeScenes();
	int count = array->count();
	if (m_currentThemeItemId >= count)
		m_currentThemeItemId = 0;
	ThemeItemScene * itemScene = (ThemeItemScene *) (array->objectAtIndex(
			m_currentThemeItemId));
	return itemScene->getThumbPath();
}

void GameData::release() {
	CC_SAFE_DELETE(g_GameData);
}

const char * GameData::getMusicPath() {
	return m_musicPath->getCString();
}
const char * GameData::getEffectPath() {
	return m_effectPath->getCString();
}
